package com.xclink.sys.service;

import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import com.xclink.common.entity.ConnomCount;
import com.xclink.common.entity.MonthEntity;
import com.xclink.common.service.AbstractService;
import com.xclink.common.utils.MD5Tools;
import com.xclink.sys.entity.User;
import com.xclink.sys.mapper.UserMapper;

import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;
@Service
public class UserService extends AbstractService<User>{
	@Autowired	
	UserMapper mapper;
	@Override
	public Mapper<User> getMapper() {
		return mapper;
	}
//	列表及模糊查询
	@Override
	public List<User> find(User entity) {
		Example example= new Example(User.class);
		example.setOrderByClause("create_time DESC");
//		模糊查询
		if(entity.getName()!=null&& !entity.getName().equals("")){
			Criteria c1=example.createCriteria();
			Criteria c2=example.createCriteria();
			c1.andLike("name", "%"+entity.getName()+"%");
			c2.andLike("username", "%"+entity.getName()+"%");
			if(entity.getUserType()!=null&&!entity.getUserType().equals("")){
				c1.andEqualTo("userType", entity.getUserType());
				c2.andEqualTo("userType", entity.getUserType());
			}
			example.or(c2);
			return mapper.selectByExample(example);
		}else{
//			列表查询 
			if(entity.getUserType()!=null&&!entity.getUserType().equals("")){
				Criteria c1=example.createCriteria();
				c1.andEqualTo("userType", entity.getUserType());
			}
			return mapper.selectByExample(example);
		}
	}
//	登录校验
	public User checkLogin(String username,String password){
		User user = mapper.selectIsExitUser(username);
		if(user!=null){
			password = MD5Tools.MD5(password+user.getSalt());
			if(user.getPassword().equals(password)){
				 return user;
			}
		}
	        return null;
	    }
/**
 * 根据用户名查询用户信息
 * @param username
 * @return
 */
	public User findUserByAccount(String username) {
		return mapper.selectIsExitUser(username);
	}
//删除或批量删除
	public int deleteAll(String[] array){
		int a =0;
		for (String i : array) {
			Long id = Long.valueOf(i);
			a=mapper.deleteByPrimaryKey(id);	
			a++;
		}
			return a;
	}
	
//	修改密码
	public boolean updatePassword(String username ,String oldPassword,String newPassword){
		User user = findUserByAccount(username);
		oldPassword = MD5Tools.MD5(oldPassword+user.getSalt());
		if (oldPassword.equals(user.getPassword())) {
			// 随机生成一个盐
			String salt = UUID.randomUUID().toString().replaceAll("-", "");
			newPassword = MD5Tools.MD5(newPassword + salt);
			user.setPassword(newPassword);
			user.setSalt(salt);
			mapper.updateByPrimaryKeySelective(user);
			}else{
				return false;
			}
			return true;
	}
	//用户统计
		public MonthEntity selectMonth(String year){
			return mapper.selectMonth(year);
		}
		public List<ConnomCount> selectM(String year) {
			return mapper.selectM(year);
		}

}
